package com.fatgirl.baseUI.visual
{
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import com.fatgirl.baseUI.IBackground;
	
	/**
	 * 背景基类
	 * 针对 可视化编程
	 * 设置某元素为背景类，可进行拖拽、层次管理 
	 * @author iceman
	 * 
	 */
	public class VBackground extends MovieClip implements IBackground
	{
		private var m_dragEnable:Boolean = true;
		public function VBackground()
		{
			super();
			this.addEventListener(MouseEvent.MOUSE_DOWN,startDragHandler);
			this.addEventListener(MouseEvent.MOUSE_UP,stopDragHandler);
			this.addEventListener(MouseEvent.CLICK,setIndexHandler);
		}
		public function set dragEnable(_b:Boolean):void
		{
			if(m_dragEnable == _b) return;
			m_dragEnable = _b;
			if(m_dragEnable)
			{
				this.addEventListener(MouseEvent.MOUSE_DOWN,startDragHandler);
				this.addEventListener(MouseEvent.MOUSE_UP,stopDragHandler);
			}
			else
			{
				this.removeEventListener(MouseEvent.MOUSE_DOWN,startDragHandler);
				this.removeEventListener(MouseEvent.MOUSE_UP,stopDragHandler);
			}
			
		}
		public function get dragEnable():Boolean
		{
			return m_dragEnable;
		}
		private function startDragHandler(e:MouseEvent):void
		{
			if(this.parent && this.parent is Sprite)
			{
				(this.parent as Sprite).startDrag();
			}
		}
		private function stopDragHandler(e:MouseEvent):void
		{
			if(this.parent && this.parent is Sprite)
			{
				(this.parent as Sprite).stopDrag();
			}
		}
		private function setIndexHandler(e:MouseEvent):void
		{
			if(this.parent)
			{
				if(this.parent.parent && this.parent.parent is DisplayObjectContainer)
				{
					(this.parent.parent as DisplayObjectContainer).setChildIndex(this.parent,(this.parent.parent as DisplayObjectContainer).numChildren-1);
				}
			}
		}
	}
}